
package JET.UtilPanels;


import JET.Centers.ControlCenter;
import JET.Centers.Graphics.GUICenter;
import JET.Centers.Graphics.GUICenter.UtilTabs;
import JET.Centers.Scripts.MyScript;
import JET.Frames.DataModels.DataModelsFactory;
import JET.Scene;
import JET.mygui.UtilTabsPane.UtilTabsPaneTab;
import java.util.ArrayList;


/*
 * UPScripts.java
 *
 * Created on 2010-08-25, 20:48:08
 */

/**
 *
 * @author 057Ry
 */
public class UPScripts extends javax.swing.JPanel implements UtilTabsPaneTab {

    static final ControlCenter CC = ControlCenter.getInstance();

    public UtilTabs getId() {
        return GUICenter.UtilTabs.Scripts;
    }

    public boolean isScrollable() {
        return false;
    }

    private Scene scene;

    /** Creates new form UPScripts */
    public UPScripts() {
        initComponents();
        scriptsList.setDragEnabled(true);
    }

    public void setScene(Scene s) {
        scene = s;
        DataModelsFactory.getScriptsListModel().setScene(s);
        repaint();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        scriptsList = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        scriptsList.setModel(DataModelsFactory.getScriptsListModel());
        scriptsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                doubleClick(evt);
            }
        });
        jScrollPane1.setViewportView(scriptsList);

        jButton1.setText("Add");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Del");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleName("Scripts");
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        newScript();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        delScripts();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void doubleClick(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_doubleClick
        if (evt.getClickCount()==2) {
            int index = scriptsList.getSelectedIndex();
            if (index != -1) {
                MyScript script = (MyScript) DataModelsFactory.getScriptsListModel().getElementAt(index);
                CC.GUIC.openScriptTab(script);
            }
        }
    }//GEN-LAST:event_doubleClick

    private void newScript() {
        if (scene!=null) {
            String name = "script"+ scene.SC.getScriptsCount() +".js";
            MyScript script = new MyScript(name);
            scene.SC.addScript(0, script);
            DataModelsFactory.getScriptsListModel().fireScriptsListChanged();
        }
    }

    private void delScripts() {
        if (scene!=null) {
            int[] indices = scriptsList.getSelectedIndices();
            if (indices.length>0) {
                ArrayList<MyScript> list = new ArrayList<MyScript>();

                for(int i : indices)
                    list.add( scene.SC.getScript(i));

                for(MyScript s : list)
                    CC.GUIC.closeScriptTab(s);

                for(MyScript s : list)
                    scene.SC.removeScript(s);

                DataModelsFactory.getScriptsListModel().fireScriptsListChanged();
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList scriptsList;
    // End of variables declaration//GEN-END:variables

}
